//
//  ShortVideoViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/14.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "ShortVideoViewController.h"
#import <BDCloudMediaPlayer/BDCloudMediaPlayerController.h>
@interface ShortVideoViewController ()<UIScrollViewDelegate>
//@property (weak, nonatomic) IBOutlet UIScrollView *botomScroView;
@property(assign)NSInteger pageIndex;
@property(assign)NSInteger psize;
@property(nonatomic,strong)NSMutableArray *dataSouce;
@property(nonatomic,strong)BDCloudMediaPlayerController *player;
@property(nonatomic,strong)NSString *playUrl;
@property(nonatomic,strong)UIScrollView *botomScroView;
@property(assign)NSInteger currentIndex;
@property(assign)BOOL ispause;
@property(nonatomic,strong)UIImageView *pauseImg;
@end

@implementation ShortVideoViewController
-(UIScrollView*)botomScroView{
    if(!_botomScroView){
        _botomScroView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height-60)];
        _botomScroView.delegate = self;
        _botomScroView.pagingEnabled = 1;
        //self.view addSubview:<#(nonnull UIView *)#>
    }
    return _botomScroView;
}
-(NSMutableArray *)dataSouce{
    if(!_dataSouce){
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
-(void)play{
    if(self.player){

        [self.player reset];
        [self.player.view removeFromSuperview];
    }
    self.player = [[BDCloudMediaPlayerController alloc] initWithContentURL:[NSURL URLWithString:self.playUrl]];
    [self.player setScalingMode:BDCloudMediaPlayerScalingModeAspectFit];
    self.player.view.backgroundColor = [UIColor whiteColor];
    [self.player setVideoDecodeMode:BDCloudMediaPlayerVideoDecodeModeSoftware];
    [self.player setBitrateIndex:1];

    self.player.view.frame = CGRectMake(0, (screen_height-60)*self.currentIndex, screen_width, screen_height-60);
        dispatch_async(dispatch_get_main_queue(), ^{
[self.botomScroView addSubview:self.player.view];
        });
    [self.player shouldAutoplay];
    [self.player enableLooping:1];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pauseplay)];
    [self.player.view addGestureRecognizer:tap];
    [self.player play];
   // [self.view addSubview:self.player.view];
   
}
-(UIImageView *)pauseImg{
    if(!_pauseImg){
         _pauseImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
       // _pauseImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"video"]];
        _pauseImg.image = [UIImage imageNamed:@"video"];
        _pauseImg.center = self.view.center;
        [self.view insertSubview:_pauseImg atIndex:100];
      //  [self.view addSubview:_pauseImg];
      //  [self.view bringSubviewToFront:_pauseImg];
        _pauseImg.hidden = 1;
    }
    return _pauseImg;
}
-(void)pauseplay{
    self.ispause = !self.ispause;
    if(self.ispause==1){
        [self.player pause];
        self.pauseImg.hidden= 0;
    }else{
        [self.player play];
        self.pauseImg.hidden= 1;
    }
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  //  self.botomScroView.alpha =1 ;
   // self.player.view.alpha = 0;
    self.pauseImg.hidden = 1;
    self.ispause = 0;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  //  self.botomScroView.alpha = 0;
    NSInteger index = scrollView.contentOffset.y/self.botomScroView.frame.size.height;
    self.currentIndex = index;
    self.playUrl = self.dataSouce[index][@"video_url"];
    [self play];
  //  self.player.view.alpha = 1;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageIndex = 1;
    self.psize = 5;
    [self loadData];
    self.currentIndex = 0;

    [self.view insertSubview:self.botomScroView atIndex:10];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(viewShow) name:@"changtoShort" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopplay) name:@"stopshort" object:nil];
    [self pauseImg];
    // Do any additional setup after loading the view from its nib.
}
-(void)stopplay{
    [self.player stop];
}
-(void)viewShow{
  [self play];
}
-(void)initScroView{
    self.botomScroView.contentSize = CGSizeMake(0, self.botomScroView.frame.size.height*self.dataSouce.count);
  //  self.playUrl =
    for(int i =0;i<self.dataSouce.count;i++){
        UIImageView *iamgesView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.botomScroView.frame.size.height*i, screen_width, self.botomScroView.frame.size.height)];
        NSURL *url= [NSURL URLWithString:self.dataSouce[i][@"thumb"]];
        [iamgesView sd_setImageWithURL:url];
        [self.botomScroView addSubview:iamgesView];
        
    }
}
-(void)loadData{
    if(![XTool GetDefaultInfo:USER_INFO]){
        return;
    }
    NSDictionary *param = @{@"ac":@"store",@"op":@"activity",@"ctrl":@"live",@"ta":@"video_list",@"page":[NSString stringWithFormat:@"%ld",self.pageIndex],@"psize":[NSString stringWithFormat:@"%ld",self.psize]};
    [self weqPostHandle:param success:^(id response) {
        if(response){
           if([response[@"data"]isKindOfClass:[NSArray class]])
           {
               NSArray *datas = response[@"data"];
            if(datas.count>0){
            [self.dataSouce addObjectsFromArray:response[@"data"]];
            self.playUrl = response[@"data"][0][@"video_url"];
            [self initScroView];
            }
           }
        //    [self play];
        }
    } failure:^(NSError *error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
